<template>
  <div class="page-search">
    <div class="search">
      <hy-form v-bind="searchFormConfig" v-model="formData">
        <template #header>
          <h1>高级检索</h1>
        </template>
        <template #footer>
          <div class="handle-btns">
            <el-button @click="handleResetClick">重置</el-button>
            <el-button type="primary" @click="handleQueryClick">搜索</el-button>
          </div>
        </template>
      </hy-form>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue'
import HyForm from '@/base-ui/form'

export default defineComponent({
  props: {
    searchFormConfig: {
      type: Object
    }
  },
  components: {
    HyForm
  },
  emits: ['resetBtnClick', 'searchBtnClick'],
  setup(props, { emit }) {
    //双向绑定的属性应该是由配置文件的field来决定的
    //1.优化一：formData中的属性应该动态决定
    const formItems = props.searchFormConfig?.formItems ?? []
    const formOriginData: any = {}

    for (const item of formItems) {
      formOriginData[`${item.field}`] = ''
    }
    const formData = ref(formOriginData)

    //2. 优化二：当用户点击重置
    const handleResetClick = () => {
      for (const key in formOriginData) {
        formData.value[`${key}`] = formOriginData[`${key}`]
      }
      emit('resetBtnClick')
    }

    //3. 用户三： 当用户点击搜索
    const handleQueryClick = () => {
      emit('searchBtnClick', formData.value)
    }

    return {
      formData,
      handleResetClick,
      handleQueryClick
    }
  }
})
</script>

<style scoped>
.handle-btns {
  text-align: right;
  padding: 0 50px 20px 0;
}
</style>
